package com.AAL.DAO;

import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IOrderInvoiceDAO;
import com.AAL.Model.Order;
import com.AAL.Model.OrderInvoice;
import com.AAL.Utils.HibernateUtil;

public class OrderInvoiceDAO implements IOrderInvoiceDAO {

	@Override
	public OrderInvoice saveAndUpdateOrderInvoice(OrderInvoice orderInvoice) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(orderInvoice);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return orderInvoice;
	}

	@Override
	public OrderInvoice deleteOrderInvoice(int orderInvoiceID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			OrderInvoice orderInvoice = (OrderInvoice) session.load(
					OrderInvoice.class, orderInvoiceID);
			if (null != orderInvoice) {
				session.delete(orderInvoice);
			}
			session.getTransaction().commit();
			return orderInvoice;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	}

	@Override
	public OrderInvoice getOrderInvoice(int orderInvoiceID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		OrderInvoice orderInvoice = (OrderInvoice) session.load(
				OrderInvoice.class, orderInvoiceID);
		return orderInvoice;
	}

	@Override
	public OrderInvoice getLstOrderInvoiceByOrder(Order order) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OrderInvoice getLstOrderInvoiceByOrderAndIsWriting(Order order) {
		// TODO Auto-generated method stub
		return null;
	}

}
